﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;

namespace HCFramework.Product.FrontEndControl
{
    public partial class ListNewProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HCDataContext db = new HCDataContext();
                List<ProductCategory> listCats = db.ProductCategories
                    .Where(ik => ik.IsActive && (M.UrlQuery.categoryID.HasValue ? ik.ParentID == M.UrlQuery.categoryID : ik.ParentID == null))
                    .OrderBy(ik => ik.OrderDisplay)
                    .ToList();
                listViewCategory.DataSource = listCats;
                listViewCategory.DataBind();
            }
        }

        protected void listViewCategory_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            string template = M.FileM.Read(@"\Product\FrontEndControl\Template\ProductItem.htm");
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Literal litCatId = e.Item.FindControl("litCatId") as Literal;
                int catId = int.Parse(litCatId.Text);
                ProductCategory category = C.ProductCategoryController.GetByID(catId);
                
                // Display products
                List<ProductCategory> listLeafCategory = C.ProductCategoryController.GetAllLeaf(category);
                Literal litProducts = e.Item.FindControl("litProducts") as Literal;
                HCDataContext db = new HCDataContext();
                var listProduct = from a in db.Products
                                  where a.IsActive && listLeafCategory.Select(ik => ik.ProductCategoryID).Contains(a.CategoryID)
                                  select a;
                if (M.UrlQuery.manufacturerID.HasValue)
                {
                    listProduct = from a in listProduct
                                  where a.ManufacturerID == M.UrlQuery.manufacturerID.Value
                                  select a;
                }
                listProduct = from a in listProduct
                              orderby a.OrderDisplay
                              select a;
                foreach (API.LINQ.Product product in listProduct.Take(6).ToList())
                {
                    litProducts.Text += M.Template.MergeProduct(template, product);
                }

                // Link category detail in header
                HyperLink linkCat = e.Item.FindControl("linkCat") as HyperLink;
                linkCat.Text = M.Common.GetLang(DataBinder.Eval(e.Item.DataItem,"Name") + "");
                string url = "";
                if (C.ProductCategoryController.IsLeafNode(catId))
                {
                    url = M.Common.GetRootAddress() + "/Product/Default.aspx";
                }
                else
                {
                    url = Request.Url.AbsoluteUri;
                }
                url = M.Http.SetPara(url, "categoryID", catId + "");
                linkCat.NavigateUrl = url;
            }
        }
    }
}